package com.by.servlet;

import com.alibaba.fastjson.JSON;
import com.by.dao.tbDao;
import com.by.entity.Dept;
import com.by.entity.tb;
import com.by.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/show")
public class tbDeptJsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        tbDao tbDao = session.getMapper(tbDao.class);
        tb tb = new tb();
        tb.setScore(50);

        Dept dept = new Dept();
        dept.setDeptid(2);
        tb.setDept(dept);

        List<tb> list = tbDao.gettbByScoreAndDept(tb);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String json = JSON.toJSONString(list);
        System.out.println(json);
        out.write(json);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
